﻿Imports System.Text.RegularExpressions
Public Class SuccObject
    Implements IComparable(Of SuccObject)
    Public code As String
    Public avail As Boolean
    Public price As Double
    Public quantity As String
    Public type As String
    Sub New(code As String, avail As Boolean, price As Double, quantity As String, type As String)
        Me.code = code
        Me.avail = avail
        Me.price = price
        If (price < 1) Then
            Me.price = 1

        End If
        Me.quantity = quantity
        Me.type = type
    End Sub
    Shared Function findAv(avail As Boolean, quantity As String, type As String) As String
        Dim av As String
        'for 'not matched'
        If quantity.Equals(SUCCOBJECT_FROMCODES) Then
            av = "0"
            'for all available
        ElseIf type = "Вася" Or type = "Кама_Кама" Or type = "Олег" Or type = "Римбат" Or type = "Белшинторг" Then
            av = "1"
        ElseIf quantity = "-1" Or quantity = "0" Or quantity = "1" Then
            'av = quantity
            av = "0"
        ElseIf avail Then
            av = "1"
        Else
            av = "0"
        End If
        findAv = av
    End Function
    Overrides Function toString() As String
        Dim av As String = findAv(avail, quantity, type)
        Dim present_price As Integer
        If price < 50000 Then
            present_price = Convert.ToInt32(Math.Round(price * Double.Parse(BYR_USD_CONV, 0), System.MidpointRounding.AwayFromZero))
        Else
            present_price = Convert.ToInt32(Math.Round(price, System.MidpointRounding.AwayFromZero))
        End If

        toString = code + vbTab + present_price.ToString + vbTab + av
    End Function
    Private Shared Function getQuantityLevel(s As String, ByRef q As Integer) As Integer
        If s = SUCCOBJECT_FROMCODES Then
            Return 2
        End If
        If s = "" Then
            Return 1
        Else
            If Not Integer.TryParse(s, q) Then

                Dim t As String = Regex.Replace(s, "[^0-9]", "")
                If Integer.TryParse(t, q) Then
                    If q = QUANTITY_THRESHOLD Then
                        If s.Chars(0) = "<" Then
                            Return 2
                        Else
                            Return 0
                        End If
                    ElseIf q > QUANTITY_THRESHOLD Then
                        Return 0
                    Else
                        Return 2
                    End If
                Else
                    q = -1
                    Return 3
                End If
            Else
                If q >= 5 Then
                    Return 0
                Else
                    Return 2
                End If
            End If
        End If
    End Function
    Public Shared Function quantityRepair(q As String) As String
        If Not q.Equals(SUCCOBJECT_FROMCODES) Then
            q = LCase(Trim(q))
            q = Regex.Replace(q, "\s", "")
            q = Regex.Replace(q, "больше", ">")
            q = Regex.Replace(q, "более", ">")
            q = Regex.Replace(q, "меньше", "<")
            q = Regex.Replace(q, "б", ">")
            If q.Length > 1 And Regex.IsMatch(q, "^[^<>0-9]") Then
                If Regex.IsMatch(q.Substring(1), "[^0-9]") Then
                    q = q.Chars(0) + Regex.Replace(q, "[^0-9]", "")
                End If
            End If
        End If
        Return q
    End Function
    Public Shared Function quantityCompare(sq1 As String, sq2 As String) As Integer
        'Level 0 - above threshold ( if quan>5 )
        'Level 1 - empty string
        'Level 2 - below threshold
        'lower level means higher priority
        sq1 = quantityRepair(sq1)
        sq2 = quantityRepair(sq2)
        Dim q1 As Integer, q2 As Integer
        Dim L1 As Integer = getQuantityLevel(sq1, q1)
        Dim L2 As Integer = getQuantityLevel(sq2, q2)
        If L1 = L2 Then
            Return 0
        ElseIf L1 > L2 Then
            Return -1
        Else
            Return 1
        End If
    End Function
    Function compareTo(other As SuccObject) As Integer Implements IComparable(Of pricegen.SuccObject).CompareTo
        If Me.Equals(other) Then
            Return 0
        Else
            Dim q As Integer = quantityCompare(Me.quantity, other.quantity) ' 1. by quantity
            If q <> 0 Then
                Return q
            Else
                If Me.price > other.price Then ' 2. by price
                    Return -1
                ElseIf Me.price < other.price Then
                    Return 1
                Else
                    Return priorityCompare(Me.type, other.type) '3. by vendor priority
                End If
            End If
        End If
    End Function

    Shared Function priorityCompare(type As String, other_type As String) As Integer
        With My.Forms.Form1.TypesBindingSource
            priorityCompare = .IndexOf(other_type) - .IndexOf(type)
        End With
    End Function

End Class